package com.ruoyi.web.controller.material;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.material.MaterialProperty;
import com.ruoyi.system.domain.material.MaterialPropertyType;
import com.ruoyi.system.service.IMaterialPropertyService;
import com.ruoyi.system.service.IMaterialPropertyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GE-LV5
 * @create 2023/3/6 16:18
 */
@Api(tags = "物料属性类型")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/material/pro")
@Slf4j
public class MaterialPropertyController extends BaseController {

    @Autowired
    IMaterialPropertyService iMaterialProperty;

    @Autowired
    IMaterialPropertyTypeService iMaterialPropertyTypeService;


    /**
     * 查询物料属性类型列表
     */
    @ApiOperation(value = "查询物料属性类型")
    @PostMapping("/list")
    public TableDataInfo list(MaterialProperty baseTeam) {
        //先查主表
        startPage();
        LambdaQueryWrapper<MaterialProperty> lqw = new LambdaQueryWrapper<MaterialProperty>();
        if (StringUtils.isNotBlank(baseTeam.getPropertyCode()) || StringUtils.isNotBlank(baseTeam.getPropertyName())) {
            lqw.eq(MaterialProperty::getPropertyCode, baseTeam.getPropertyCode()).or()
                    .eq(MaterialProperty::getPropertyName, baseTeam.getPropertyName());
        }
        List<MaterialProperty> list = iMaterialProperty.list(lqw);
        //子表
        if (!list.isEmpty()) {
            for (MaterialProperty materialProperty : list) {
                LambdaQueryWrapper<MaterialPropertyType> materialPropertyTypeLambdaQueryWrapper = new LambdaQueryWrapper<MaterialPropertyType>();
                materialPropertyTypeLambdaQueryWrapper.eq(MaterialPropertyType::getMaterialPropertyId,materialProperty.getId());
                List<MaterialPropertyType> materialPropertyTypes = iMaterialPropertyTypeService.list(materialPropertyTypeLambdaQueryWrapper);
                materialProperty.setMaterialPropertyType(materialPropertyTypes);
            }
        }
        return getDataTable(list);
    }


    /**
     * 新增属性类型
     */
    @Log(title = "物料分组", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增物料属性类型")
    @PostMapping
    public AjaxResult add(@RequestBody MaterialProperty baseTeam) {
        return toAjax(iMaterialProperty.save(baseTeam) ? 1 : 0);
    }

    /**
     * 修改物料属性类型
     */
    @Log(title = "物料分组", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改物料属性类型")
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialProperty baseTeam) {
        return toAjax(iMaterialProperty.updateById(baseTeam) ? 1 : 0);
    }

    /**
     * 删除物料属性类型
     */
    @Log(title = "物料分组", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除物料属性类型")
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iMaterialProperty.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }

}
